import Cookies from "js-cookie";

/**
 * 浏览器永久缓存
 */
export const Local = {
  set(key: string, val: any) {
    window.localStorage.setItem(key, JSON.stringify(val));
  },
  get(key: string) {
    let json: any = window.localStorage.getItem(key);
    return JSON.parse(json);
  },
  remove(key: string) {
    window.localStorage.removeItem(key);
  },
  clear() {
    window.localStorage.clear();
  },
};

/**
 * 浏览器临时缓存
 */
export const Session = {
  set(key: string, val: any) {
    if (key === "token" || key === "refresh_token")
      return Cookies.set(key, val);
    window.sessionStorage.setItem(key, JSON.stringify(val));
  },
  get(key: string) {
    if (key === "token" || key === "refresh_token") return Cookies.get(key);
    let json: any = window.sessionStorage.getItem(key);
    return JSON.parse(json);
  },
  remove(key: string) {
    if (key === "token" || key === "refresh_token") return Cookies.remove(key);
    window.sessionStorage.removeItem(key);
  },
  clear() {
    Cookies.remove("token");
    Cookies.remove("refresh_token");
    window.sessionStorage.clear();
  },
};
